package com.climber.android.commonres.widget.loading

import android.annotation.SuppressLint
import android.view.View
import android.widget.FrameLayout
import com.climber.android.commonres.R
import io.ganguo.library.mvp.util.UIUtils
import io.library.android.loading.Gloading

/**
 * Created by Mikes at 2019/4/14
 */
@SuppressLint("ViewConstructor")
class GlobalLoadingStatusView(private val holder: Gloading.Holder) : FrameLayout(holder.context) {
    private val mContext = holder.context
    private val mClickCallBack: Gloading.GloadingStatusClickListener? = holder.statusClick

    init {
        setBackgroundColor(UIUtils.getColor(R.color.common_bg_content))
    }

    fun setStatus(status: Int, data : Any?=null) {
        removeAllViews()

        var show = true
        when (status) {
            STATUS_LOADING -> {
                val commonBigLoadingView = CommonBigLoadingView(mContext)
                val loadingData = data as? CommonLoadingData
                if(loadingData != null){
                    commonBigLoadingView.configure(loadingContent = loadingData.loadingContent)

                    if(loadingData.bgColor != null){
                        setBackgroundColor(loadingData.bgColor)
                    }
                }
                commonBigLoadingView.setOnClickListener{
                    mClickCallBack?.clickWithStatus(status)
                }
                addView(commonBigLoadingView)
            }
            STATUS_SUCCESS -> show = false
            STATUS_RETRY -> {
                val commonBigRetryView = CommonBigRetryView(mContext)
                val retryData = data as? CommonRetryData
                if(retryData != null){
                    commonBigRetryView.configure(retryContent = retryData.retryContent)

                    if(retryData.bgColor != null){
                        setBackgroundColor(retryData.bgColor)
                    }
                }

                commonBigRetryView.setRetryListener {
                    mClickCallBack?.clickWithStatus(status)
                }
                addView(commonBigRetryView)
            }
            STATUS_EMPTY -> {
                val commonBigEmptyView = CommonBigEmptyView(mContext)
                val emptyData = data as? CommonEmptyData
                if(emptyData != null){
                    commonBigEmptyView.configure(emptyContent = emptyData.emptyContent,
                            emptyIcon = emptyData.emptyIcon,
                            btnLabel = emptyData.btnLabel)

                    if(emptyData.bgColor != null){
                        setBackgroundColor(emptyData.bgColor)
                    }
                }

                commonBigEmptyView.setEmptyListener{
                    mClickCallBack?.clickWithStatus(status)
                }

                addView(commonBigEmptyView)
            }
        }

        visibility = if (show) View.VISIBLE else View.GONE
    }

    companion object {
        const val STATUS_LOADING = Gloading.STATUS_LOADING
        const val STATUS_SUCCESS = Gloading.STATUS_LOAD_SUCCESS
        const val STATUS_RETRY = Gloading.STATUS_LOAD_FAILED
        const val STATUS_EMPTY = Gloading.STATUS_EMPTY_DATA
    }
}
